package com.xinyue.game.service.framwork.context;

import com.alibaba.fastjson.JSON;
import com.xinyue.cross.server.server.ServerResponseService;
import com.xinyue.game.common.IServerError;
import com.xinyue.game.message.GameServerMessageModel;
import com.xinyue.game.message.GameServerRequestHeader;
import com.xinyue.game.message.GameServerResponseHeader;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.helpers.MessageFormatter;

import java.nio.charset.StandardCharsets;

public class GameUserChannelContext {
    private static Logger logger = LoggerFactory.getLogger(GameUserChannelContext.class);
    private GameServerMessageModel<GameServerRequestHeader> requestParam;
    private GameUserChannel gameUserChannel;
    private ChannelHandlerContext ctx;


    public GameUserChannelContext(ChannelHandlerContext ctx, GameServerMessageModel<GameServerRequestHeader> requestParam, GameUserChannel gameUserChannel) {
        this.requestParam = requestParam;
        this.ctx = ctx;
        this.gameUserChannel = gameUserChannel;
    }

    public GameServerRequestHeader getHeader() {
        return this.requestParam.getHeader();
    }

    public <T> T getRequestBody(Class<T> clazz) {
        ByteBuf byteBuf = this.requestParam.getBody();
        T data = null;
        if (byteBuf.isReadable()) {
            // 读取剩余的可以读取的字节数量
            byte[] bytes = new byte[byteBuf.readableBytes()];
            byteBuf.readBytes(bytes);
            String json = new String(bytes, StandardCharsets.UTF_8);
            logger.debug("Request Body--> {}", json);
            data = JSON.parseObject(json, clazz);
        } else {
            logger.debug("Request Body--> null");
        }

        return data;
    }

    public void response(Object body) {
        GameServerMessageModel<GameServerResponseHeader> responseModel = ServerResponseService.createResponseModel(this.requestParam.getHeader());
        if (body != null) {
            String json = JSON.toJSONString(body);
            byte[] data = json.getBytes(StandardCharsets.UTF_8);
            ByteBuf byteBuf = Unpooled.wrappedBuffer(data);
            responseModel.setBody(byteBuf);
            responseModel.setBodyStr(json);
        }
        ctx.writeAndFlush(responseModel);
    }

    public void response(IServerError error) {
        GameServerMessageModel<GameServerResponseHeader> responseModel = ServerResponseService.createResponseModel(this.requestParam.getHeader(), error.getErrorCode(), error.getErrorMsg());
        ctx.writeAndFlush(responseModel);
    }

    public void response(IServerError error, String format, Object... args) {
        String errorDesc = MessageFormatter.arrayFormat(format, args).toString();
        GameServerMessageModel<GameServerResponseHeader> responseModel = ServerResponseService.createResponseModel(this.requestParam.getHeader(), error.getErrorCode(), errorDesc);
        ctx.writeAndFlush(responseModel);
    }

}
